#!/usr/bin/env python
'''
Given a fasta file containing many sequences and a specific string
, the function get the sequence(s) with tag(s) containing the specific string.
'''

__author__ = "Yangho chen"
__email__ = "yanghoch@usc.edu"
__modification__ = "Created on Aug 16, 2009"

from parse_fasta_file import get_seqs_from_fasta_w_substr

import os
import getopt
import sys
import string

def usage():
    sys.stderr.write( "Working dir is " + os.getcwd() + '\n' )
    print "usage: ", sys.argv[0]
    print "The four parameters should be "
    print "(1) fasta file"
    print "(2) contig (gene) (substring in fasta tag)"
    print "(3) T if only one sequence is required)"
    print "(4) The output file name"
    print " Be careful if multiple genes contains the same substring"
    sys.exit( 2 )

def main():
    try:
        opts, args = getopt.getopt( sys.argv[1:], 'h', ["help"] )
    except getopt.error:
        usage()
        sys.exit( 2 );
    # process options and argument 
    for o in opts:
        if o in ( "-h", "--help" ):
            usage()
            sys.exit();
    if len( args ) == 4 :
        faFileN = args[0]
        contigN = args[1]
        bSingleContig = args[2]
        ofileN = args[3]
        if ( bSingleContig == 'T' ):
            get_seqs_from_fasta_w_substr( faFileN, contigN, True, ofileN )
        else:
            get_seqs_from_fasta_w_substr( faFileN, contigN, False, ofileN )
    else:
        print  string.join( args, ' ' )
        print  str( len( args ) ) + " args are provided."
        usage()

if __name__ == '__main__':
    main()
